﻿using System.Collections.Generic;

namespace Gonte.Workflow
{
    public class TaskContext
    {
        #region Methods

        public object GetLocal(string name)
        {
            if (0 == _locals.Count)
            {
                return null;
            }

            return _locals.Peek()[name]; 
        }

        public void SetLocal(string name, object obj)
        {
            _locals.Peek()[name] = obj;
        }

        public void CreateLocalScope()
        {
            _locals.Push(new Dictionary<string, object>());
        }

        public void DestroyLocalScope()
        {
            // TODO: If the contained objects are disposable. call DIspose in all of them
            Dictionary<string, object> local = _locals.Pop();
            local = null;
        } 

        #endregion

        #region Properties

        /// <summary>
        /// The shared instances of the objects visible to all the tasks
        /// </summary>
        public Dictionary<string, object> Globals
        {
            get { return _globals; }
            set { _globals = value; }
        }


        #endregion

        #region Fields

        private Dictionary<string, object> _globals = new Dictionary<string, object>();
        private Stack<Dictionary<string, object>> _locals = new Stack<Dictionary<string, object>>();

        #endregion
    }
}
